package org.xworker.model.html;

import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.lang.util.StringUtils;

public class Html {
    public static String toHtml(ActionContext actionContext) {
        Thing self = actionContext.getObject("self");

        StringBuilder sb = new StringBuilder("<html>\n");
        sb.append("    <head>\n");
        sb.append("        <title>").append(self.getMetadata().getName()).append("</title>\n");
        sb.append("    </head>\n");
        sb.append("    <body>\n");

        for (Thing child : self.getChilds()) {
            String html = child.doAction("toHtml", actionContext);
            if (html != null) {
                html = StringUtils.addLeftPadding(html, null, "        ");
                sb.append(html).append("\n");
            }
        }

        sb.append("    </body>\n");
        sb.append("</html>");

        return sb.toString();
    }
}
